<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>vue3+vite用proxy解决跨域及proxy原理解析 | Jaho’s Blog</title>
    <meta name="generator" content="VuePress 1.8.0">
    <link rel="icon" href="/favicon.ico">
    <link rel="manifest" href="/manifest.json">
    <meta name="description" content="记录日常学习工作">
    
    <link rel="preload" href="/assets/css/0.styles.5b8fe0a4.css" as="style"><link rel="preload" href="/assets/js/app.6514719e.js" as="script"><link rel="preload" href="/assets/js/2.ea114ffb.js" as="script"><link rel="preload" href="/assets/js/47.3df1bfdb.js" as="script"><link rel="preload" href="/assets/js/4.e093c564.js" as="script"><link rel="prefetch" href="/assets/js/10.13203aa5.js"><link rel="prefetch" href="/assets/js/11.099868c4.js"><link rel="prefetch" href="/assets/js/12.c8365e17.js"><link rel="prefetch" href="/assets/js/13.89751e7c.js"><link rel="prefetch" href="/assets/js/14.31f563eb.js"><link rel="prefetch" href="/assets/js/15.08b381b6.js"><link rel="prefetch" href="/assets/js/16.1a3867de.js"><link rel="prefetch" href="/assets/js/17.7dd4e0c6.js"><link rel="prefetch" href="/assets/js/18.907c4597.js"><link rel="prefetch" href="/assets/js/19.fac8aa1a.js"><link rel="prefetch" href="/assets/js/20.73fe0e75.js"><link rel="prefetch" href="/assets/js/21.5c50aa1c.js"><link rel="prefetch" href="/assets/js/22.83d03663.js"><link rel="prefetch" href="/assets/js/23.061e7ac0.js"><link rel="prefetch" href="/assets/js/24.cc7e9011.js"><link rel="prefetch" href="/assets/js/25.fa6c7aef.js"><link rel="prefetch" href="/assets/js/26.39392f70.js"><link rel="prefetch" href="/assets/js/27.204436be.js"><link rel="prefetch" href="/assets/js/28.3f54c157.js"><link rel="prefetch" href="/assets/js/29.799d048c.js"><link rel="prefetch" href="/assets/js/3.6c5d6214.js"><link rel="prefetch" href="/assets/js/30.deed02f3.js"><link rel="prefetch" href="/assets/js/31.5916873b.js"><link rel="prefetch" href="/assets/js/32.b0634144.js"><link rel="prefetch" href="/assets/js/33.734675b8.js"><link rel="prefetch" href="/assets/js/34.20404dc4.js"><link rel="prefetch" href="/assets/js/35.d93b829f.js"><link rel="prefetch" href="/assets/js/36.b0692d8d.js"><link rel="prefetch" href="/assets/js/37.3dffa306.js"><link rel="prefetch" href="/assets/js/38.04649a97.js"><link rel="prefetch" href="/assets/js/39.7dfcdc32.js"><link rel="prefetch" href="/assets/js/40.04914511.js"><link rel="prefetch" href="/assets/js/41.ced85449.js"><link rel="prefetch" href="/assets/js/42.72739213.js"><link rel="prefetch" href="/assets/js/43.de1cff16.js"><link rel="prefetch" href="/assets/js/44.48e301ae.js"><link rel="prefetch" href="/assets/js/45.870a7650.js"><link rel="prefetch" href="/assets/js/46.657f15b8.js"><link rel="prefetch" href="/assets/js/48.f4e4e774.js"><link rel="prefetch" href="/assets/js/49.632bf611.js"><link rel="prefetch" href="/assets/js/5.6531e181.js"><link rel="prefetch" href="/assets/js/50.a8018493.js"><link rel="prefetch" href="/assets/js/51.cb424f59.js"><link rel="prefetch" href="/assets/js/52.a9f595dc.js"><link rel="prefetch" href="/assets/js/53.cf8a9ec0.js"><link rel="prefetch" href="/assets/js/54.46d774a3.js"><link rel="prefetch" href="/assets/js/55.3574f9b8.js"><link rel="prefetch" href="/assets/js/56.baf60ba5.js"><link rel="prefetch" href="/assets/js/57.af42b559.js"><link rel="prefetch" href="/assets/js/58.9300cb59.js"><link rel="prefetch" href="/assets/js/6.00f437ec.js"><link rel="prefetch" href="/assets/js/7.58ac7166.js"><link rel="prefetch" href="/assets/js/8.1b9c5002.js"><link rel="prefetch" href="/assets/js/9.5c076362.js">
    <link rel="stylesheet" href="/assets/css/0.styles.5b8fe0a4.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="Jaho’s Blog" class="logo"> <span class="site-name can-hide">Jaho’s Blog</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/posts/" class="nav-link router-link-active">
  技术博客
</a></div> <a href="https://juejin.cn/user/26836627369806" target="_blank" rel="noopener noreferrer" class="repo-link">
    个人主页
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/posts/" class="nav-link router-link-active">
  技术博客
</a></div> <a href="https://juejin.cn/user/26836627369806" target="_blank" rel="noopener noreferrer" class="repo-link">
    个人主页
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><a href="/posts/" aria-current="page" class="sidebar-link">写在前面</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>工作问题解决</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/posts/项目问题解决/vuevite.html" class="active sidebar-link">vue3+vite用proxy解决跨域及proxy原理解析</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/posts/项目问题解决/大文件.html" class="sidebar-link">大文件切片上传和断点续传</a></li><li><a href="/posts/项目问题解决/代码优化.html" class="sidebar-link">实际工作中代码优化</a></li><li><a href="/posts/项目问题解决/组件问题.html" class="sidebar-link">解决组件传值问题</a></li><li><a href="/posts/项目问题解决/动态组件.html" class="sidebar-link">动态组件实践</a></li><li><a href="/posts/项目问题解决/大屏.html" class="sidebar-link">大屏九宫格视频</a></li><li><a href="/posts/项目问题解决/滚动.html" class="sidebar-link">Vue中Element表单定位滚动到校验错误位置</a></li><li><a href="/posts/项目问题解决/红绿灯.html" class="sidebar-link">Promise实现动态红绿灯切换</a></li><li><a href="/posts/项目问题解决/懒加载.html" class="sidebar-link">实现元素懒加载</a></li><li><a href="/posts/项目问题解决/小程序组件.html" class="sidebar-link">微信小程序封装组件</a></li><li><a href="/posts/项目问题解决/axios.html" class="sidebar-link">项目中对axios的整体封装</a></li><li><a href="/posts/项目问题解决/docker.html" class="sidebar-link">docker实践(附windows安装下载教程)</a></li><li><a href="/posts/项目问题解决/eltable.html" class="sidebar-link">el-table自定义表头及无数据状态</a></li><li><a href="/posts/项目问题解决/gojs.html" class="sidebar-link">gojs入门学习</a></li><li><a href="/posts/项目问题解决/react踩坑.html" class="sidebar-link">React踩坑</a></li><li><a href="/posts/项目问题解决/小程序踩坑.html" class="sidebar-link">小程序踩坑</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>JavaScript</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Css</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Nodejs</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Vue</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Git</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="vue3-vite用proxy解决跨域及proxy原理解析"><a href="#vue3-vite用proxy解决跨域及proxy原理解析" class="header-anchor">#</a> vue3+vite用proxy解决跨域及proxy原理解析</h1> <h3 id="前言"><a href="#前言" class="header-anchor">#</a> 前言</h3> <p>前端跨域的方式是面试八股文中常见的一部分，最近面试中也是被问到跨域的问题，跨域的解决方案无非就是几个</p> <ol><li>Jsonp跨域</li> <li>cors</li> <li>vue中的proxy跨域
本地项目中调试用的最多的就是 node代理</li></ol> <h3 id="复习一下什么是跨域"><a href="#复习一下什么是跨域" class="header-anchor">#</a> 复习一下什么是跨域</h3> <blockquote><p>浏览器从一个网页去请求另一个资源时，域名、端口、协议任一不同，都是跨域。在前后端分离的模式下，前后端的域名是不一致的，跨域是必然发生的事情。但服务器与服务器之间请求数据并不会存在跨域行为，跨域行为是浏览器安全策略限制，也称为浏览器的同源策略</p></blockquote> <p>如果是跨域，你将会发现下图的报错，经典的跨域问题，前端和后端都是我的本地项目，但仅仅是端口的不同，浏览器请求就发生了跨域的问题</p> <div class="language-js extra-class"><pre class="language-js"><code>Access to XMLHttpRequest at <span class="token string">'http://127.0.0.1:8888/'</span> from origin <span class="token string">'http://127.0.0.1:3000'</span> has been blocked by <span class="token constant">CORS</span> <span class="token literal-property property">policy</span><span class="token operator">:</span> No <span class="token string">'Access-Control-Allow-Origin'</span> header is present on the requested resource<span class="token punctuation">.</span>
</code></pre></div><h3 id="先开启一个后端node服务"><a href="#先开启一个后端node服务" class="header-anchor">#</a> 先开启一个后端node服务</h3> <p>由于需要用到一个后端接口来测试是否能够发生跨域效果，我们利用node开启一个服务，在vue项目之外开启一个node文件，然后执行以下代码</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">//node1.js</span>

<span class="token keyword">var</span> http <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;http&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
http<span class="token punctuation">.</span><span class="token function">createServer</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">request<span class="token punctuation">,</span> response</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 发送 HTTP 头部 </span>
    <span class="token comment">// HTTP 状态值: 200 : OK</span>
    <span class="token comment">// 内容类型: text/plain</span>
    response<span class="token punctuation">.</span><span class="token function">writeHead</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string-property property">'Content-Type'</span><span class="token operator">:</span> <span class="token string">'text/plain'</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 发送响应数据 &quot;Hello World&quot;</span>
    response<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token string">'Hello World\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token number">8888</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 终端打印如下信息</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Server running at http://127.0.0.1:8888/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>vscode中可以直接下载一个工具用来跑node文件，直接右键就能运行,非常的方便</p> <p><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e489fdc35a5643d98091fee6b2b093e2~tplv-k3u1fbpfcp-watermark.image?" alt="image.png"></p> <p><img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/eb3c3b23663c4484896a79f8a881492f~tplv-k3u1fbpfcp-watermark.image?" alt="image.png"></p> <p>如果没有安装的同学可以执行命令 node node1.js 进行执行，服务开启后成功如图</p> <p><img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a13e6f14bc8c48feb3bd21914818b356~tplv-k3u1fbpfcp-watermark.image?" alt="image.png"></p> <p>我们再在浏览器打开服务端口进行查看返回的结果测试一下</p> <p><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/83fc95aa655b4b598cf239067727990d~tplv-k3u1fbpfcp-watermark.image?" alt="image.png"></p> <p>到这里我们的后端node服务已经能够启动了，那就说明可以当做一个接口来进行调用然后测试我们的跨域了</p> <h3 id="封装一个ajax"><a href="#封装一个ajax" class="header-anchor">#</a> 封装一个ajax</h3> <p>首先我们要发送请求，所以我们需要利用到ajax，这里引入axios太麻烦，所以直接用原生的ajax来发送请求，顺便学习一下如何手写一个原生的ajax请求</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">ajax</span><span class="token punctuation">(</span><span class="token parameter">options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//创建XMLHttpRequest对象</span>
    <span class="token keyword">const</span> xhr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">XMLHttpRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span>


    <span class="token comment">//初始化参数的内容</span>
    options <span class="token operator">=</span> options <span class="token operator">||</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
    options<span class="token punctuation">.</span>type <span class="token operator">=</span> <span class="token punctuation">(</span>options<span class="token punctuation">.</span>type <span class="token operator">||</span> <span class="token string">'GET'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    options<span class="token punctuation">.</span>dataType <span class="token operator">=</span> options<span class="token punctuation">.</span>dataType <span class="token operator">||</span> <span class="token string">'json'</span>
    <span class="token keyword">const</span> params <span class="token operator">=</span> options<span class="token punctuation">.</span>data

    <span class="token comment">//发送请求</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>options<span class="token punctuation">.</span>type <span class="token operator">===</span> <span class="token string">'GET'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        xhr<span class="token punctuation">.</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">'GET'</span><span class="token punctuation">,</span> options<span class="token punctuation">.</span>url <span class="token operator">+</span> <span class="token string">'?'</span> <span class="token operator">+</span> params<span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span>
        xhr<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>options<span class="token punctuation">.</span>type <span class="token operator">===</span> <span class="token string">'POST'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        xhr<span class="token punctuation">.</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">'POST'</span><span class="token punctuation">,</span> options<span class="token punctuation">.</span>url<span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span>
        xhr<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>params<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//接收请求</span>
    xhr<span class="token punctuation">.</span><span class="token function-variable function">onreadystatechange</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>xhr<span class="token punctuation">.</span>readyState <span class="token operator">===</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">let</span> status <span class="token operator">=</span> xhr<span class="token punctuation">.</span>status
            <span class="token keyword">if</span> <span class="token punctuation">(</span>status <span class="token operator">&gt;=</span> <span class="token number">200</span> <span class="token operator">&amp;&amp;</span> status <span class="token operator">&lt;</span> <span class="token number">300</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                options<span class="token punctuation">.</span>success <span class="token operator">&amp;&amp;</span> options<span class="token punctuation">.</span><span class="token function">success</span><span class="token punctuation">(</span>xhr<span class="token punctuation">.</span>responseText<span class="token punctuation">,</span> xhr<span class="token punctuation">.</span>responseXML<span class="token punctuation">)</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                options<span class="token punctuation">.</span>fail <span class="token operator">&amp;&amp;</span> options<span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span>status<span class="token punctuation">)</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>对我们的ajax请求进行一个简单的封装，这样就算是一个比较简单的手写的ajax，从request对象的创建到参数初始化，发送请求及接受相应都比较完整</p> <h3 id="发送一个ajax请求"><a href="#发送一个ajax请求" class="header-anchor">#</a> 发送一个ajax请求</h3> <p>在我们的vue项目中执行一个todo函数，放置我们请求的ajax函数，对应我们刚才启动的node服务进行请求，测试是否会发生跨域问题</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">todo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token function">ajax</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    <span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">'post'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">dataType</span><span class="token operator">:</span> <span class="token string">'json'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">data</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token literal-property property">url</span><span class="token operator">:</span> <span class="token string">'http://127.0.0.1:8888'</span><span class="token punctuation">,</span>
    <span class="token comment">// url: '/api',</span>
    <span class="token function-variable function">success</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">text<span class="token punctuation">,</span>xml</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//请求成功后的回调函数</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token function-variable function">fail</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">status</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">////请求失败后的回调函数</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>status<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>对函数执行后发现无可置疑，肯定是发生了跨域</p> <p><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a2f68547dbc046c6a8ab89e5fe9440fe~tplv-k3u1fbpfcp-watermark.image?" alt="image.png"></p> <p>由于端口的不同， vue项目跑在了3000端口，mode服务跑在了8888端口，所以发生了跨域的问题</p> <p><img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/102be6e419e3467b9505f9c304bcf639~tplv-k3u1fbpfcp-watermark.image?" alt="image.png"></p> <p><code>这是配置代理跨域前的请求，记住这个格式待会和代理成功的做一个对比</code></p> <h3 id="配置proxy"><a href="#配置proxy" class="header-anchor">#</a> 配置proxy</h3> <p>由于项目中使用的是vue3+vite包，所以我们打开vite.config.js</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> defineConfig <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vite'</span>
<span class="token keyword">import</span> vue <span class="token keyword">from</span> <span class="token string">'@vitejs/plugin-vue'</span>
<span class="token keyword">import</span> styleImport<span class="token punctuation">,</span> <span class="token punctuation">{</span> VantResolve <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vite-plugin-style-import'</span><span class="token punctuation">;</span>
<span class="token comment">// https://vitejs.dev/config/</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token function">defineConfig</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token literal-property property">plugins</span><span class="token operator">:</span> <span class="token punctuation">[</span>
    <span class="token function">vue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token function">styleImport</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
      <span class="token literal-property property">resolves</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token function">VantResolve</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token literal-property property">server</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token comment">//主要是加上这段代码</span>
      <span class="token literal-property property">host</span><span class="token operator">:</span> <span class="token string">'127.0.0.1'</span><span class="token punctuation">,</span>
      <span class="token literal-property property">port</span><span class="token operator">:</span> <span class="token number">3000</span><span class="token punctuation">,</span>
      <span class="token literal-property property">proxy</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token string-property property">'/api'</span><span class="token operator">:</span> <span class="token punctuation">{</span>
          <span class="token literal-property property">target</span><span class="token operator">:</span> <span class="token string">'http://127.0.0.1:8888'</span><span class="token punctuation">,</span>	<span class="token comment">//实际请求地址</span>
          <span class="token literal-property property">changeOrigin</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
          <span class="token function-variable function">rewrite</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">path</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> path<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^\/api</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

</code></pre></div><p>这个是代理成功的请求会是这个样子,我们发送的127.0.0.1:8888变成了127.0.0.1:3000,所以我们的代理生效了，不发跨域了，因为我们请求的和本地vue项目没有发送同源策略(请求地址和端口都是相同的)，<code>但是为什么我们请求成功了呢？</code>我们要请求的明明是8888端口才对呀？让我们看下面的解析
<img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a05f0fc22ff5424c899edfb5d93d3e79~tplv-k3u1fbpfcp-watermark.image?" alt="image.png"><br>
从上图中，server是我们新增的部分，我们每一个配置都来解析一下</p> <ul><li>host指的是将vue项目跑在哪个地址上</li> <li>prot则指的是vue项目跑的端口拉
如果不加这段配置的话，我们npm run dev的时候vue项目就不会跑在127.0.0.1:3000上，而是会自动分配一个地址和端口</li> <li>proxy 其实就是利用了 Node 代理</li> <li>target是我们实际要请求的地址</li> <li>rewrite则是检测接口中出现的'api/'因为这段字符只是我们用来检测转发的而已，实际请求中要用正则将api设置为空，要记住他只是一个识别字符而已，因为我们的项目中可能存在需要请求不同的后端接口地址</li></ul> <p>意思就是识别到请求 ‘api/’ 就会变成 http://127.0.0.1:8888'</p> <p>我们要调用的接口是 http://127.0.0.1:8888 <br>
但我们的项目是在127.0.0.1:3000所以proxy会做如下代理进行转发<br>
127.0.0.1:3000/api/ -&gt; http://127.0.0.1:8888<br>
如果还有后续地址的话
127.0.0.1:3000/api/test/test1 -&gt; http://127.0.0.1::8888/test/test1<br></p> <h3 id="总结"><a href="#总结" class="header-anchor">#</a> 总结</h3> <p>proxy其实就是因为浏览器同源协议无法请求非同源的地址，但是服务器直接没有同源协议，利用将本地请求转到本地服务器进行代理转发，从而绕过了同源协议的限制，通过代理的实现可以解决跨域的问题，换了个思路去解决实际产生的问题，实在是一个比较好的解决方法</p> <p>参考<a href="https://blog.csdn.net/weixin_43972437/article/details/107291071" target="_blank" rel="noopener noreferrer">文章<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2023-01-04 10:56:12</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/posts/" class="prev router-link-active">
        写在前面
      </a></span> <span class="next"><a href="/posts/项目问题解决/大文件.html">
        大文件切片上传和断点续传
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/assets/js/app.6514719e.js" defer></script><script src="/assets/js/2.ea114ffb.js" defer></script><script src="/assets/js/47.3df1bfdb.js" defer></script><script src="/assets/js/4.e093c564.js" defer></script>
  </body>
</html>
